package com.wanxi.springboot.team.manage.system.service.impl;

import com.wanxi.springboot.team.manage.system.api.CommonResult;
import com.wanxi.springboot.team.manage.system.mapper.TeamMapper;
import com.wanxi.springboot.team.manage.system.mapper.UserMapper;
import com.wanxi.springboot.team.manage.system.model.ChangeUserTeamRecord;
import com.wanxi.springboot.team.manage.system.model.Team;
import com.wanxi.springboot.team.manage.system.model.User;
import com.wanxi.springboot.team.manage.system.model.UserTeam;
import com.wanxi.springboot.team.manage.system.mapper.UserTeamMapper;
import com.wanxi.springboot.team.manage.system.service.ChangeUserTeamRecordService;
import com.wanxi.springboot.team.manage.system.service.UserTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 蒋帆
 * @since 2020-12-21
 */
@Service
public class UserTeamServiceImpl extends ServiceImpl<UserTeamMapper, UserTeam> implements UserTeamService {

    @Autowired(required = false)
    UserTeamMapper userTeamMapper;
    @Autowired(required = false)
    ChangeUserTeamRecordService changeUserTeamRecordService;
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired(required = false)
    TeamMapper teamMapper;

    @Override
    public CommonResult setUserTeam(Map map) {
        try {
            //获取小组id
            Integer teamId = Integer.valueOf((String) map.get("teamId"));
            //获取user_team中的user主键集合
            List<Integer> ids = userTeamMapper.getUserIds();
            String teamName = teamMapper.selectTeamById(teamId).getName();
            //获取移除小组的成员id
            String left = ((String) map.get("left")).replace("\"", "");
            List<ChangeUserTeamRecord> changeUserTeamRecords = new ArrayList<>();
            if (!left.equals("[]")) {//如果左边有成员变动（即有成员被移除小组）
                //获取移除成员的id集合
                String[] leftSplit = left.substring(1, left.length() - 1).split(",");
                System.out.println(leftSplit);
                List<Integer> leftList = new ArrayList<>();
                for (String str : leftSplit) {//逐个取出并将其转换为int类型
                    int num = Integer.parseInt(str);//生成成员调组记录
                    ChangeUserTeamRecord changeUserTeamRecord = new ChangeUserTeamRecord();
                    User user = userMapper.selectUserById(num);
                    String userName = user.getCode();
                    if (user.getName()!=null)
                        userName += "("+user.getName()+")";
                    changeUserTeamRecord.setUser(userName).setPreviousTeam(teamName).setNowTeam("暂未分组");
                    leftList.add(num);
                    changeUserTeamRecords.add(changeUserTeamRecord);
                }
                if (leftList.size() > 0) {//如果集合中有值，修改用户所在小组
                    userTeamMapper.editUserTeam(leftList, 0);//被移除小组，只需将其小组id变为空
                }
            }
            //获取放入小组的成员id
            String right = ((String) map.get("right")).replace("\"", "");
            if (!right.equals("[]")) {
                String[] rightSplit = right.substring(1, right.length() - 1).split(",");
                System.out.println(rightSplit);
                List<Integer> addRightList = new ArrayList<>();
                List<Integer> editRightList = new ArrayList<>();
                for (String str : rightSplit) {
                    int num = Integer.parseInt(str);
                    ChangeUserTeamRecord changeUserTeamRecord = new ChangeUserTeamRecord();
                    User user = userMapper.selectUserById(num);
                    String userName = user.getCode();
                    if (user.getName()!=null)
                        userName += "("+user.getName()+")";
                    changeUserTeamRecord.setUser(userName).setPreviousTeam("暂未分组").setNowTeam(teamName);
                    if (ids.contains(num))
                        editRightList.add(num);
                    else
                        addRightList.add(num);
                    changeUserTeamRecords.add(changeUserTeamRecord);
                }
                if (editRightList.size() > 0)
                    userTeamMapper.editUserTeam(editRightList, teamId);
                if (addRightList.size() > 0)
                    userTeamMapper.addUserTeam(addRightList, teamId);
            }
            teamMapper.setCrew(teamId);//更新小组人数
            //添加调组记录
            changeUserTeamRecordService.addChangeUserTeamRecord(changeUserTeamRecords);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("分配失败");
        }
//        int result = userTeamService.setUserTeam(ids,teamId);
        return CommonResult.success(true,"分配成功");
    }
}
